﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class DatChoDAO
    {
        #region SelectALL
        public static List<DatChoDTO> SelectALLDatCho()
        {
            List<DatChoDTO> list = new List<DatChoDTO>();
            try
            {
                DataTable dt = DataProvider.ExecuteQuery("sp_SelectALLDatCho");
                foreach (DataRow dr in dt.Rows)
                {
                    DatChoDTO dc = new DatChoDTO();
                    dc.MACHUYEN = dr["MACHUYEN"].ToString();
                    dc.MAKH = dr["MAKH"].ToString();
                    dc.NGAYDI = DateTime.Parse(dr["NGAYDI"].ToString());
                    dc.SOLUONG = int.Parse(dr["SOLUONG"].ToString());
                    dc.TINHTRANG = bool.Parse(dr["TINHTRANG"].ToString());
                    dc.SOHIEU = dr["SOHIEU"].ToString();
                    list.Add(dc);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
        #endregion
        public static List<DatChoDTO> SelectALLDatChobyKhachHang(String maKH)
        {
            List<DatChoDTO> ds = new List<DatChoDTO>();
            SqlConnection conn = DataProvider.ConnectDB();
            String sql = "select * from DatCho dc where MAKH=@maKH";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter pa = new SqlParameter("@maKH", System.Data.SqlDbType.VarChar);
            pa.Value = maKH;
            cmd.Parameters.Add(pa);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                DatChoDTO dc = new DatChoDTO();
                dc.MAKH = dr.GetString(0);
                dc.NGAYDI = dr.GetDateTime(1);
                dc.MACHUYEN = dr.GetString(2);
                dc.SOHIEU = dr.GetString(3);
                dc.SOLUONG = dr.GetInt32(4);
                dc.TINHTRANG = dr.GetBoolean(5);
                ds.Add(dc);
            }
            conn.Close();
            return ds;
        }
        public static bool ThemDatCho(String makhachhang, DateTime ngaydi, String machuyen, String sohieu, Int32 soluong)
        {
            bool result = false;

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ma", makhachhang));
                sqlParams.Add(new SqlParameter("@ngaydi", ngaydi));
                sqlParams.Add(new SqlParameter("@machuyen", machuyen));
                sqlParams.Add(new SqlParameter("@sohieu", sohieu));
                sqlParams.Add(new SqlParameter("@soluong", soluong));
                // Call Store Procedure

                int n = DataProvider.ExecuteNoneQuery("usp_themdatcho", sqlParams);

                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool SuaLichDi1(DateTime ngaydi, String machuyen, String sohieu)
        {
            bool result = false;

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ngaydi", ngaydi));
                sqlParams.Add(new SqlParameter("@machuyen", machuyen));
                sqlParams.Add(new SqlParameter("@sohieu", sohieu));
                // Call Store Procedure                 
                int n = DataProvider.ExecuteNoneQuery("usp_sualichdi1", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool SuaDatCho(String makhachhang, DateTime ngaydi, String machuyen, String sohieu, Int32 soluong)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ma", makhachhang));
                sqlParams.Add(new SqlParameter("@ngaydi", ngaydi));
                sqlParams.Add(new SqlParameter("@machuyen", machuyen));
                sqlParams.Add(new SqlParameter("@sohieu", sohieu));
                sqlParams.Add(new SqlParameter("@soluong", soluong));
                // Call Store Procedure

                int n = DataProvider.ExecuteNoneQuery("usp_suadatcho", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool XoaDatCho(String makhachhang, DateTime ngaydi, String machuyen, String sohieu, Int32 soluong)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ma", makhachhang));
                sqlParams.Add(new SqlParameter("@ngaydi", ngaydi));
                sqlParams.Add(new SqlParameter("@machuyen", machuyen));
                sqlParams.Add(new SqlParameter("@sohieu", sohieu));
                sqlParams.Add(new SqlParameter("@soluong", soluong));
                // Call Store Procedure

                int n = DataProvider.ExecuteNoneQuery("usp_xoadatcho", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #region Tang/Giam Hang Hoa
        public static void thayDoiHangHoa(String maKH, String maChuyen, String ngayDi, int khoiLuong, String soHieu)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter pa = new SqlParameter("@maKH", SqlDbType.VarChar);
            pa.Value = maKH;
            cmd.Parameters.Add(pa);

            pa = new SqlParameter("@maChuyen", SqlDbType.VarChar);
            pa.Value = maChuyen;
            cmd.Parameters.Add(pa);

            pa = new SqlParameter("@ngayDi", SqlDbType.VarChar);
            pa.Value = ngayDi;
            cmd.Parameters.Add(pa);

            pa = new SqlParameter("@khoiLuong", SqlDbType.Int);
            pa.Value = khoiLuong;
            cmd.Parameters.Add(pa);

            pa = new SqlParameter("@soHieu", SqlDbType.VarChar);
            pa.Value = soHieu;
            cmd.Parameters.Add(pa);

            cmd.CommandText = "usp_giamhanghoa";
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            conn.Close();
        }
        #endregion
        #region select dat cho by primary key
        public static DatChoDTO SelectDatChobyPrimaryKey(String maKH, String maChuyen, String ngayDi, String soHieu)
        {
            DatChoDTO dc = new DatChoDTO();

            SqlConnection conn = DataProvider.ConnectDB();
            String sql = "select * from DatCho where MAKH=@maKH and CONVERT(varchar(20),NGAYDI,103)=@ngayDi and MaChuyen=@maChuyen and SoHieu=@soHieu";
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlParameter pa = new SqlParameter("@maKH", System.Data.SqlDbType.VarChar);
            pa.Value = maKH;
            cmd.Parameters.Add(pa);

            pa = new SqlParameter("@maChuyen", SqlDbType.VarChar);
            pa.Value = maChuyen;
            cmd.Parameters.Add(pa);

            pa = new SqlParameter("@ngayDi", SqlDbType.VarChar);
            pa.Value = ngayDi;
            cmd.Parameters.Add(pa);

            pa = new SqlParameter("@soHieu", SqlDbType.VarChar);
            pa.Value = soHieu;
            cmd.Parameters.Add(pa);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                dc.MAKH = dr.GetString(0);
                dc.NGAYDI = dr.GetDateTime(1);
                dc.MACHUYEN = dr.GetString(2);
                dc.SOHIEU = dr.GetString(3);
                dc.SOLUONG = dr.GetInt32(4);
                dc.TINHTRANG = dr.GetBoolean(5);
            }
            conn.Close();
            return dc;
        }
        #endregion
    }
}
